package id.co.shuba.spamobile.task;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import id.co.shuba.spamobile.App;
import id.co.shuba.spamobile.Encryptor;
import id.co.shuba.spamobile.Sessions;
import id.co.shuba.spamobile.data.AreaDB;
import id.co.shuba.spamobile.data.DSARDB;
import id.co.shuba.spamobile.data.DatabaseHandler;
import id.co.shuba.spamobile.data.MaintenanceVisitDB;
import id.co.shuba.spamobile.data.StandardDB;
import id.co.shuba.spamobile.data.ProspectDB;
import id.co.shuba.spamobile.data.ProspectProgressDB;
import id.co.shuba.spamobile.model.DSARInfo;
import id.co.shuba.spamobile.model.MaintenanceVisitInfo;
import id.co.shuba.spamobile.model.ProspectInfo;
import id.co.shuba.spamobile.model.ProspectProgressInfo;
import id.co.shuba.spamobile.model.UserInfo;
import id.co.shuba.spamobile.services.WebServiceHelper;
import id.co.shuba.spamobile.util.Base64;
import id.co.shuba.spamobile.util.XmlUtil;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Path.FillType;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Xml;

public class DownloadOfflineTask extends BaseProgressTask{	
	SQLiteDatabase _db;			
	String ns = null;
	boolean deleteOnComplete = true;
	int progress = 0;
	int i = 0;
	int totalRow = 0;
	OnDownloadCompleteListener _listener;
	

	public DownloadOfflineTask(SQLiteDatabase db) {	
		_db = db;										
	}
	
	public void setOnDownloadCompleteListener(OnDownloadCompleteListener listener) {
		_listener = listener;
	}
	
	private void publishProgress(String status) {
		progress = (int)(i * 100f /totalRow);		
		publishProgress(progress);
		publishStatus(String.format("Updating %s...%s/%s", status, String.valueOf(i), String.valueOf(totalRow)));
	}	
	
	private void readData(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {       
        parser.require(XmlPullParser.START_TAG, ns, "SPA");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();            
            if (name.equals("TBL_PROSPECT_COUNT")) {
            	totalRow += XmlUtil.readInt(parser, "TBL_PROSPECT_COUNT");
            } else if (name.equals("TBL_PROSPECT_VISIT_COUNT")) {
            	totalRow += XmlUtil.readInt(parser, "TBL_PROSPECT_VISIT_COUNT");
            } else if (name.equals("TBL_PROSPECT_PROGRESS_COUNT")) {
            	totalRow += XmlUtil.readInt(parser, "TBL_PROSPECT_PROGRESS_COUNT");
            } else if (name.equals("TBL_MAINTENANCE_VISIT_COUNT")) {
            	totalRow += XmlUtil.readInt(parser, "TBL_MAINTENANCE_VISIT_COUNT");
            } else if (name.equals("TBL_PROSPECT")) {
                readTblProspect(parser);                
            } else if (name.equals("TBL_PROSPECT_VISIT")) {
            	readTblProspectVisit(parser);
            } else if (name.equals("TBL_PROSPECT_PROGRESS")) {
            	readTblProspectProgress(parser);
            } else if (name.equals("TBL_MAINTENANCE_VISIT")) {
            	readTblMaintenanceVisit(parser);
            } else {
                skip(parser);
            }
        }      
    }
	
	private void readTblProspect(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {						
        parser.require(XmlPullParser.START_TAG, ns, "TBL_PROSPECT");              
        
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();            
            if (name.equals("PROSPECT")) {
                readProspect(parser);
            } else {
                skip(parser);
            }
        }      
    }
	
	private void readTblProspectVisit(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {		
        parser.require(XmlPullParser.START_TAG, ns, "TBL_PROSPECT_VISIT");               
        
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();            
            if (name.equals("PROSPECT_VISIT")) {
                readProspectVisit(parser);                
            } else {
                skip(parser);
            }
        }      
    }
	
	private void readTblProspectProgress(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {		
        parser.require(XmlPullParser.START_TAG, ns, "TBL_PROSPECT_PROGRESS");              
        
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();            
            if (name.equals("PROSPECT_PROGRESS")) {
                readProspectProgress(parser);
            } else {
                skip(parser);
            }
        }      
    }
	
	private void readTblMaintenanceVisit(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {       			
		parser.require(XmlPullParser.START_TAG, ns, "TBL_MAINTENANCE_VISIT");			
        
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();            
            if (name.equals("MAINTENANCE_VISIT")) {
                readMaintenanceVisit(parser);
            } else {
                skip(parser);
            }
        }      
    }
	
	private long readLong(XmlPullParser parser, String tag) throws IOException, XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, tag);
        String text = readText(parser);
        long val = 0;
        try {
			val = Long.parseLong(text);
		} catch (Exception e) {		
		}        
        parser.require(XmlPullParser.END_TAG, ns, tag);
        return val;
    }
	
	private int readInt(XmlPullParser parser, String tag) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, tag);
        String text = readText(parser);
        int val = 0;
        try {
			val = Integer.parseInt(text);
		} catch (Exception e) {		
		}        
        parser.require(XmlPullParser.END_TAG, ns, tag);
        return val;
    }
	
	private String readString(XmlPullParser parser, String tag) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, tag);
        String val = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, tag);
        return val;
    }
	
	private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }
	
	private void readProspect(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {       
        parser.require(XmlPullParser.START_TAG, ns, "PROSPECT");
        
        ProspectInfo info = new ProspectInfo();
        
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();            
            if (name.equals("PROSPECT_ID")) info.setId(readString(parser, "PROSPECT_ID"));     
            else if (name.equals("PROSPECT_NO")) info.setProspectNo(readString(parser, "PROSPECT_NO"));
            else if (name.equals("FULL_NAME")) info.setFullName(readString(parser, "FULL_NAME"));
            else if (name.equals("JENIS_USAHA")) info.setJenisUsaha(readString(parser, "JENIS_USAHA"));
            else if (name.equals("RENCANA_PLAFON")) info.setRencanaPlafon(readLong(parser, "RENCANA_PLAFON"));
            else if (name.equals("MOBILE_PHONE")) info.setMobilePhone(readString(parser, "MOBILE_PHONE"));
            else if (name.equals("OFFICE_PHONE")) info.setOfficePhone(readString(parser, "OFFICE_PHONE"));
            else if (name.equals("RESIDENTIAL_PHONE")) info.setResidentialPhone(readString(parser, "RESIDENTIAL_PHONE"));
            else if (name.equals("KODYA_KAB_BUSINESS")) info.setKodyaKabBusiness(readString(parser, "KODYA_KAB_BUSINESS"));
            else if (name.equals("KECAMATAN_BUSINESS")) info.setKecamatanBusiness(readString(parser, "KECAMATAN_BUSINESS"));
            else if (name.equals("KELURAHAN_BUSINESS")) info.setKelurahanBusiness(readString(parser, "KELURAHAN_BUSINESS"));
            else if (name.equals("PROSPECT_TYPE")) info.setProspectType(readString(parser, "PROSPECT_TYPE"));
            else if (name.equals("DEBTOR_TYPE")) info.setDebtorType(readString(parser, "DEBTOR_TYPE"));
            else if (name.equals("CHANNELING")) info.setChanneling(readString(parser, "CHANNELING"));
            else if (name.equals("ZIP_CODE_BUSINESS")) info.setZipCodeBusiness(readString(parser, "ZIP_CODE_BUSINESS"));
            else if (name.equals("ADDRESS_BUSINESS")) info.setAddressBusiness(readString(parser, "ADDRESS_BUSINESS"));
            else if (name.equals("EKSTERNAL")) info.setEksternal(readString(parser, "EKSTERNAL"));
            else if (name.equals("REFERENSI")) info.setReferensi(readString(parser, "REFERENSI"));
            else if (name.equals("AVG_EXPENSE")) info.setAvgExpense(readLong(parser, "AVG_EXPENSE"));
            else if (name.equals("DATE_OF_BIRTH")) info.setDateOfBirth(readString(parser, "DATE_OF_BIRTH"));
            else if (name.equals("MARITAL_STATUS")) info.setMaritalStatus(readString(parser, "MARITAL_STATUS"));
            else if (name.equals("SPOUSE_NAME")) info.setSpouseName(readString(parser, "SPOUSE_NAME"));
            else if (name.equals("JML_TANGGUNGAN")) info.setJmlTanggungan(readInt(parser, "JML_TANGGUNGAN"));
            else if (name.equals("PRODUCT_CODE")) info.setProductCode(readString(parser, "PRODUCT_CODE"));
            else if (name.equals("KTP_NO")) info.setKtpNo(readString(parser, "KTP_NO"));
            else if (name.equals("KTP_NAME")) info.setKtpName(readString(parser, "KTP_NAME"));
            else if (name.equals("ENTRY_BY")) info.setEntryBy(readString(parser, "ENTRY_BY"));
            else if (name.equals("ENTRY_DATE")) info.setEntryDate(readString(parser, "ENTRY_DATE"));
            else if (name.equals("UPDATE_BY")) info.setUpdateBy(readString(parser, "UPDATE_BY"));
            else if (name.equals("UPDATE_DATE")) info.setUpdateDate(readString(parser, "UPDATE_DATE"));
            else if (name.equals("UPLOAD_BY")) info.setUploadBy(readString(parser, "UPLOAD_BY"));
            else if (name.equals("UPLOAD_DATE")) info.setUploadDate(readString(parser, "UPLOAD_DATE"));
            else if (name.equals("IS_UPLOAD")) info.setIsUpload(readString(parser, "IS_UPLOAD"));
            else if (name.equals("AO_CODE")) info.setAoCode(readString(parser, "AO_CODE"));
            else if (name.equals("UNIT_CODE")) info.setUnitCode(readString(parser, "UNIT_CODE"));
            else if (name.equals("PROGRESS_STATUS")) info.setProgressStatus(readString(parser, "PROGRESS_STATUS"));
            else if (name.equals("UM_DECISION")) info.setUmDecision(readString(parser, "UM_DECISION"));
            else if (name.equals("UM_DECISION_NOTE")) info.setUmDecisionNote(readString(parser, "UM_DECISION_NOTE"));
            else if (name.equals("UM_DECISION_DATE")) info.setUmDecisionNote(readString(parser, "UM_DECISION_DATE"));
            else if (name.equals("REPUTASI_DEBITUR")) info.setReputasiDebitur(readString(parser, "REPUTASI_DEBITUR"));
            else if (name.equals("CO_ASSIGNED")) info.setCoAssigned(readString(parser, "CO_ASSIGNED"));
            else if (name.equals("ALTERNATE_FOR")) info.setAlternateFor(readString(parser, "ALTERNATE_FOR"));
            else if (name.equals("NOT_INTERESTED_REASON")) info.setNotInterestedReason(readString(parser, "NOT_INTERESTED_REASON"));
            else if (name.equals("GU_CLOSE_DEAL")) info.setGuCloseDeal(readString(parser, "GU_CLOSE_DEAL"));
            else if (name.equals("IS_DELETED")) info.setIsDeleted(readString(parser, "IS_DELETED"));
            else if (name.equals("IS_OFFLINE_ENTRY")) info.setIsOfflineEntry(readString(parser, "IS_OFFLINE_ENTRY"));
            else if (name.equals("LOAN_NUMBER")) info.setLoanNumber(readString(parser, "LOAN_NUMBER"));
            else if (name.equals("CIF_NUMBER")) info.setCIFNumber(readInt(parser, "CIF_NUMBER"));
            else if (name.equals("OMZET_1")) info.setOmzet1(readLong(parser, "OMZET_1"));
            else if (name.equals("OMZET_2")) info.setOmzet2(readLong(parser, "OMZET_2"));
            else if (name.equals("OMZET_3")) info.setOmzet3(readLong(parser, "OMZET_3"));
            else if (name.equals("OMZET_4")) info.setOmzet4(readLong(parser, "OMZET_4"));
            else if (name.equals("OMZET_5")) info.setOmzet5(readLong(parser, "OMZET_5"));
            else if (name.equals("OMZET_6")) info.setOmzet6(readLong(parser, "OMZET_6"));
            else if (name.equals("OMZET_7")) info.setOmzet7(readLong(parser, "OMZET_7"));
            else if (name.equals("OMZET_8")) info.setOmzet8(readLong(parser, "OMZET_8"));
            else if (name.equals("OMZET_9")) info.setOmzet9(readLong(parser, "OMZET_9"));
            else if (name.equals("OMZET_10")) info.setOmzet10(readLong(parser, "OMZET_10"));
            else if (name.equals("OMZET_11")) info.setOmzet11(readLong(parser, "OMZET_11"));
            else if (name.equals("OMZET_12")) info.setOmzet12(readLong(parser, "OMZET_12"));
            else if (name.equals("OMZET_AVERAGE")) info.setOmzetAverage(readLong(parser, "OMZET_AVERAGE"));
            else if (name.equals("SOURCE_CODE")) info.setSourceCode(readString(parser, "SOURCE_CODE"));
            else if (name.equals("PIPELINE_START_TIME")) info.setPipelineStartTime(readString(parser, "PIPELINE_START_TIME"));
            else if (name.equals("PIPELINE_FINISH_TIME")) info.setPipelineFinishTime(readString(parser, "PIPELINE_FINISH_TIME"));
            else if (name.equals("PROSPECT_START_TIME")) info.setProspectStartTime(readString(parser, "PROSPECT_START_TIME"));
            else if (name.equals("PROSPECT_FINISH_TIME")) info.setProspectFinishTime(readString(parser, "PROSPECT_FINISH_TIME"));
            else if (name.equals("IS_PIPELINE_KP")) info.setIsPipelineKP(readString(parser, "IS_PIPELINE_KP"));
            else if (name.equals("GU_FUP")) info.setGuFup(readString(parser, "GU_FUP"));
            else if (name.equals("FLAG_FREEZE")) info.setFlagFreeze(readInt(parser, "FLAG_FREEZE"));
            else if (name.equals("PLACE_OF_BIRTH")) info.setPlaceOfBirth(readString(parser, "PLACE_OF_BIRTH"));
            else if (name.equals("NIP")) info.setNIP(readString(parser, "NIP"));
            else if (name.equals("KARAKTER")) info.setKarakter(readString(parser, "KARAKTER"));
            else skip(parser);                                   
        }      
        
        ProspectDB.addOrUpdate(_db, info, false);
        
        i++;
        publishProgress("Prospect");		
    }	
	
	private void readProspectVisit(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {       
        parser.require(XmlPullParser.START_TAG, ns, "PROSPECT_VISIT");
        
        DSARInfo info = new DSARInfo();
        
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();            
            if (name.equals("PROSPECT_ID")) info.setProspectID(readString(parser, "PROSPECT_ID"));
            else if (name.equals("VISIT_NO")) info.setVisitNo(readInt(parser, "VISIT_NO"));
            else if (name.equals("DATE_VISIT_PLAN")) info.setDateVisitPlan(readString(parser, "DATE_VISIT_PLAN"));
            else if (name.equals("DATE_VISIT_ACTUAL")) info.setDateVisitActual(readString(parser, "DATE_VISIT_ACTUAL"));
            else if (name.equals("JOINT_FIELD_WORK")) info.setJoinFieldWork(readString(parser, "JOINT_FIELD_WORK"));
            else if (name.equals("OBJECTIVE_OF_VISIT")) info.setObjectiveOfVisit(readString(parser, "OBJECTIVE_OF_VISIT"));
            else if (name.equals("RESULT_OF_VISIT")) info.setResultOfVisit(readString(parser, "RESULT_OF_VISIT"));
            else if (name.equals("REMARK")) info.setRemark(readString(parser, "REMARK"));            
            else if (name.equals("ENTRY_BY")) info.setEntryBy(readString(parser, "ENTRY_BY"));
            else if (name.equals("ENTRY_DATE")) info.setEntryDate(readString(parser, "ENTRY_DATE"));
            else if (name.equals("UPDATE_BY")) info.setUpdateBy(readString(parser, "UPDATE_BY"));
            else if (name.equals("UPDATE_DATE")) info.setUpdateDate(readString(parser, "UPDATE_DATE"));
            else if (name.equals("UPLOAD_BY")) info.setUploadBy(readString(parser, "UPLOAD_BY"));
            else if (name.equals("UPLOAD_DATE")) info.setUploadDate(readString(parser, "UPLOAD_DATE"));
            else if (name.equals("IS_UPLOAD")) info.setIsUpload(readString(parser, "IS_UPLOAD"));      
            else if (name.equals("IS_DELETED")) info.setIsDeleted(readString(parser, "IS_DELETED"));      
            else skip(parser);                                   
        }      
        
        DSARDB.addOrUpdate(_db, info);
        
        i++;
        publishProgress("Prospect Visit");	
    }	
	
	private void readProspectProgress(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {       
        parser.require(XmlPullParser.START_TAG, ns, "PROSPECT_PROGRESS");
        
        ProspectProgressInfo info = new ProspectProgressInfo();
        
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();            
            if (name.equals("PROSPECT_ID")) info.setProspectID(readString(parser, "PROSPECT_ID"));            
            else if (name.equals("PROGRESS_STATUS")) info.setProgressStatus(readString(parser, "PROGRESS_STATUS"));                       
            else if (name.equals("ENTRY_BY")) info.setEntryBy(readString(parser, "ENTRY_BY"));
            else if (name.equals("ENTRY_DATE")) info.setEntryDate(readString(parser, "ENTRY_DATE"));
            else if (name.equals("UPDATE_BY")) info.setUpdateBy(readString(parser, "UPDATE_BY"));
            else if (name.equals("UPDATE_DATE")) info.setUpdateDate(readString(parser, "UPDATE_DATE"));
            else if (name.equals("UPLOAD_BY")) info.setUploadBy(readString(parser, "UPLOAD_BY"));
            else if (name.equals("UPLOAD_DATE")) info.setUploadDate(readString(parser, "UPLOAD_DATE"));
            else if (name.equals("IS_UPLOAD")) info.setIsUpload(readString(parser, "IS_UPLOAD"));                                   
            else skip(parser);                                   
        }      
        
        ProspectProgressDB.addOrUpdate(_db, info);
        
        i++;
        publishProgress("Progress");
    }
	
	private void readMaintenanceVisit(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException {       
        parser.require(XmlPullParser.START_TAG, ns, "MAINTENANCE_VISIT");
        
        MaintenanceVisitInfo info = new MaintenanceVisitInfo();
        
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();            
            if (name.equals("VISIT_ID")) info.setId(readString(parser, "VISIT_ID"));            
            else if (name.equals("AO_CODE")) info.setAoCode(readString(parser, "AO_CODE"));
            else if (name.equals("UNIT_CODE")) info.setUnitCode(readString(parser, "UNIT_CODE"));
            else if (name.equals("DATE_VISIT")) info.setDateVisit(readString(parser, "DATE_VISIT"));
            else if (name.equals("NO_ACCOUNT")) info.setNoAccount(readString(parser, "NO_ACCOUNT"));
            else if (name.equals("DEBTOR_NAME")) info.setDebtorName(readString(parser, "DEBTOR_NAME"));
            else if (name.equals("OBJECTIVE_OF_VISIT")) info.setObjectiveOfVisit(readString(parser, "OBJECTIVE_OF_VISIT"));            
            else if (name.equals("ENTRY_BY")) info.setEntryBy(readString(parser, "ENTRY_BY"));
            else if (name.equals("ENTRY_DATE")) info.setEntryDate(readString(parser, "ENTRY_DATE"));
            else if (name.equals("UPDATE_BY")) info.setUpdateBy(readString(parser, "UPDATE_BY"));
            else if (name.equals("UPDATE_DATE")) info.setUpdateDate(readString(parser, "UPDATE_DATE"));
            else if (name.equals("UPLOAD_BY")) info.setUploadBy(readString(parser, "UPLOAD_BY"));
            else if (name.equals("UPLOAD_DATE")) info.setUploadDate(readString(parser, "UPLOAD_DATE"));
            else if (name.equals("IS_UPLOAD")) info.setIsUpload(readString(parser, "IS_UPLOAD")); 
            else if (name.equals("IS_DELETED")) info.setIsDeleted(readString(parser, "IS_DELETED")); 
            else skip(parser);                                   
        }      
        
        MaintenanceVisitDB.addOrUpdate(_db, info);
        
        i++;
        publishProgress("Maintenance Visit");
    }
	
	private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                    depth--;
                    break;
            case XmlPullParser.START_TAG:
                    depth++;
                    break;
            }
        }
    }
		
	@Override
	protected AsyncTaskResult<Void> doInBackground(Object... params) {		
		boolean success = false;
		File file = (File)params[0];
		FileInputStream fis = null;
		GZIPInputStream gis = null;
		try {					
			String nip = (String)params[1];
			/*
			SecretKeySpec localSecretKeySpec = new SecretKeySpec(
					App.createCompleteKey(nip).getBytes(), Encryptor.ALGO_AES);
			
		    Cipher localCipher = Cipher.getInstance(Encryptor.ENC_AES);
		    IvParameterSpec ivspec = new IvParameterSpec(
					App.IV.getBytes());
		    localCipher.init(Cipher.ENCRYPT_MODE, localSecretKeySpec, ivspec);

		    CipherInputStream in = new CipherInputStream(new FileInputStream((File)params[0]), localCipher);*/							
			
			fis = new FileInputStream(file);
			gis = new GZIPInputStream(fis);
			XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(gis, null);
            parser.nextTag();            					
			
			readData(parser);			
			
			publishProgress(100);
			publishStatus("Done!");
			
			success = true;
			return new AsyncTaskResult<Void>((Void) null);
		} catch (Exception e) {
								
			
			
			publishProgress(0);
			publishStatus(e.getMessage());
			return new AsyncTaskResult<Void>(e);
		} finally {
			try {
				if (gis != null) gis.close();
				if (fis != null) fis.close();
				if (success && deleteOnComplete)
					file.delete();
			} catch (Exception e2) {}
			
		}
	}		
	
	@Override
	protected void onPostExecute(AsyncTaskResult<Void> result) {
		if (_listener != null) {
			_listener.onDownloadComplete(result.getError());
		}
	}
	
	@Override
	public String getTitle() {		
		return "DownloadOfflineTask";
	}
	
	
	@Override
	protected void onCancelled(AsyncTaskResult<Void> result) {		
		super.onCancelled(result);
		
		Log.d("DownloadOfflineTask", "Task cancelled");
	}	
}
